package Sokoban;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.io.LineNumberReader;

 
public class Ranking implements Comparable {
   
   private int score;
   private int level;
 
   public Ranking(int levelNo, int result)
   {
	   level = levelNo;
	   score = result;
   }
 
   public int getScore()
   {
	   return score;
   }
 
   public int getLevel()
   {
	   return level;
   }
 
   public static void bestSave(Ranking[] best)
   {
	   File file = new File("Ranking.txt");
	   try {
		   BufferedWriter save = new BufferedWriter(new FileWriter(file));
		   for (int i = 0; i < best.length; i++)
		   {
			   save.write(best[i].toString());
			   save.newLine();
		   }
		   save.close();
	   }
	   catch (IOException ex)
	   {
		   System.out.println(ex.getMessage());
	   }
   }
   public static int countLines(File aFile) throws IOException
   {
	   LineNumberReader reader = null;
	   try {
		   reader = new LineNumberReader(new FileReader (aFile));
		   while ((reader.readLine())!= null );
		   return reader.getLineNumber();
	   } finally {
		   if ( reader!=null )
			   reader.close();
	   }
   } 
   
   public static Ranking[] bestLoad(String fileName)
   {
	   if (! new File(fileName).exists()) {
			throw new IllegalArgumentException("Plik " + fileName + " nie istnieje.");
		}
	   else 
	   {
	   File file = new File(fileName);
	   //Ranking[] best = new Ranking[numOfScores];
	   int lines;
	try {
		lines = countLines(file);
	} catch (IOException e) {
		
		System.out.println("Nie udało się otworzyć pliku" + fileName); return null;
	}
	   if (lines == 0) return null;
	   else
	   {
	   Ranking[] best = new Ranking[lines];
	   try
	   {
		   Scanner load = new Scanner(new FileReader(fileName));
		  
		   
		   for (int j = 0; j < lines ; j++)
		   {
			   
			   int level = load.nextInt();
			   int score = load.nextInt();
			   best[j] = new Ranking(level, score);
		   }
		   load.close();
	   }
	   catch (IOException ex)
	   {
		   System.out.println("Nie znaleziono pliku " + fileName);
	   }
	   
	   return best;
	   }
	   }
   }
 
   public static void makeRanking(int levelNo, int result)
   {
	   Ranking[] tmp = bestLoad("Ranking.txt");
	   try {
	   Arrays.sort(tmp);
	   } catch (IllegalArgumentException ie ){}
	   int length = tmp.length;
	   if (length < RunnableMyFrame.numOfScores) //tmp[RunnableMyFrame.numOfScores-1].getScore() < Points.getAllPoints())
	   {
		   Ranking newRecord = new Ranking(levelNo, result);
		   tmp = new Ranking[length+1];
		   tmp[length] = newRecord;
		   Arrays.sort(tmp);
		   bestSave(tmp);
		   
	   }
	   else if (tmp[RunnableMyFrame.numOfScores-1].getLevel() < levelNo &&  tmp[RunnableMyFrame.numOfScores-1].getScore() < result)
	   {
		   Ranking newRecord = new Ranking(levelNo, result);
		   tmp[tmp.length] = newRecord;
		   Arrays.sort(tmp);
		   bestSave(tmp);
	   }
   }
   public String toString()
   {
	   return getLevel() + " " + getScore();
   }
 
   public int compareTo(Object obj)
   {
	   Ranking tmp = (Ranking) obj;
	 
	   if (getLevel() < tmp.getLevel())
		   return 1;
	   if (getLevel() == tmp.getLevel() && getScore() > tmp.getScore())
		   return 1;
	   if (getScore() == tmp.getScore() & getLevel() == tmp.getLevel()) 
		   return 0;
	   return 1;
   }
}
